package com.frxuexi.sign.view

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.widget.DatePicker
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.entity.SignBeanItem
import com.frxuexi.lib_base.entity.VisitStatus
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.sign.SignViewModel
import com.frxuexi.sign.adapter.SignAdapter
import com.frxuexi.sign.adapter.SignStatusAdapter
import com.frxuexi.sign.databinding.ActivitySignListBinding
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@Route(path = "/sign/SignListActivity")
@AndroidEntryPoint
class SignListActivity : BaseActivity<ActivitySignListBinding,SignViewModel>() {
    val signAdapter by lazy { SignAdapter() }
    val signStatusAdapter by lazy { SignStatusAdapter() }
    lateinit var signList:ArrayList<SignBeanItem>
    var search=false
    override fun initData() {
        vm.signListData.observe(this){
            signList=it
            signAdapter.addData(it)
            signAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.signlistRv.apply {
            adapter=signAdapter
            layoutManager=LinearLayoutManager(this@SignListActivity)
        }
        vm.getSigns(SPUtils.getInstance().getInt(Const.UID))

        val list = listOf<VisitStatus>(
            VisitStatus("全部", true),
            VisitStatus("正常", false),
            VisitStatus("迟到", false),
            VisitStatus("早退", false),
            VisitStatus("缺勤", false)
        )

        signStatusAdapter.addData(list)
        binding.signlistRvStatus.apply {
            adapter=signStatusAdapter
            layoutManager=LinearLayoutManager(this@SignListActivity,RecyclerView.HORIZONTAL,false)
        }

        signStatusAdapter.setOnItemClickListener { adapter, view, position ->
            signStatusAdapter.data.forEach {
                it.isCheck=false
            }
            val visitStatus = signStatusAdapter.data.get(position)
            visitStatus.isCheck=true
            signStatusAdapter.notifyDataSetChanged()

            signAdapter.data.clear()
            if(position!=0){
                signAdapter.addData(signList.filter { it.status== visitStatus.status})
            }else{//全部
                signAdapter.addData(signList)
            }
            signAdapter.notifyDataSetChanged()
        }
        val today=vm.getDate(System.currentTimeMillis())

        binding.signlistFilter.setOnClickListener {
            val calendar = Calendar.getInstance()
            DatePickerDialog(this,object :OnDateSetListener{
                override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
                    val m=if(month+1<10) "0${month+1}" else "${month+1}"
                    val day=if(dayOfMonth<10) "0${dayOfMonth}" else "${dayOfMonth}"
                    val date="${year}-${m}-${day}"
                    signAdapter.data.clear()
                    if(!search) {
                        binding.signlistEt.setText("${date}至")
                        signAdapter.addData(signList.filter {
                            if(it.morningCheckIn.isNotEmpty()) {
                                it.morningCheckIn.split(" ")[0]  in date ..today
                            }else {
                                it.afternoonCheckIn.split(" ")[0]  in date ..today
                            }
                        })

                    }else{
                        binding.signlistEt.setText("${binding.signlistEt.text.toString()}${date}")
                        val split = binding.signlistEt.text.toString().split("至")
                        signAdapter.addData(signList.filter {
                            if(it.morningCheckIn.isNotEmpty()) {
                                it.morningCheckIn.split(" ")[0] in split[0] ..split[1]
                            }else {
                                it.afternoonCheckIn.split(" ")[0]  in split[0] ..split[1]
                            }
                        })
                    }
                    signAdapter.notifyDataSetChanged()
                    search=!search
                }
            },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH))
                .show()


        }
    }

}